package cn.TS.servlet;

import cn.TS.model.Login;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    public void init(ServletConfig config) throws ServletException {
        // TODO Auto-generated method stub
        super.init(config);
        try {
            Class.forName("com.mysql.Driver");
        } catch (Exception e) {
            // TODO: handle exception
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        Connection con;
        PreparedStatement sql;
        Login log = new Login();
        String logname = request.getParameter("logname"),
                password = request.getParameter("password");
        if (logname == null)
            logname = "";
        if (password == null)
            password = "";
        boolean isLD = true;
        boolean boo = logname.length() > 0 && password.length() > 0 && isLD;
        String backnews = "";
        try {
            String url = "jdbc:mysql://localhost:3306/db_login" +
                    "user=root&&password=123456&&characterEncoding=utf-8";
            con = DriverManager.getConnection(url);
            String insertCondition = "INSERT INTO login VALUES (?,?)";
            sql = con.prepareStatement(insertCondition);

            if (boo) {
                sql.setString(1, logname);
                sql.setString(2, password);
                int m = sql.executeUpdate();
                if (m != 0) {
                    backnews = "��¼�ɹ�";
                    log.setBacknews(backnews);
                    log.setLogname(logname);
                    log.setPassword(password);
                }
            } else {
                backnews = "��¼ʧ��";
                log.setBacknews(backnews);
            }
            sql.close();
            con.close();
        } catch (SQLException e) {

        }
        RequestDispatcher dispatcher = request.getRequestDispatcher("resources/show.jsp");
        dispatcher.forward(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
